﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using SharingCenter.Web.Classes.enums;

namespace SharingCenter.Web.Classes
{
    using SharingCenter.Web.Classes.Profiles;
    public class AvatarImage : Image
    {
        protected Guid _userId;
        bool useridSet = false;
        protected AvatarType _type = AvatarType.Normal;
        public bool AddHash { get; set; }

        public Guid UserId 
        {
            get { return this._userId; }
            set 
            { 
                this._userId = value;
                useridSet = true;
            }
        }

        public AvatarType AvatarType 
        {
            get { return this._type; }
            set { this._type = value; }
        }

        public AvatarImage()
            : base()
        {
            this.PreRender += new EventHandler(AvatarImage_PreRender);
        }

        void AvatarImage_PreRender(object sender, EventArgs e)
        {
            if (Context.User.Identity.IsAuthenticated && !useridSet)
            {
                _userId = WebProfile.Current.UserId;
            }

            var format = AddHash ? "{1}?tt={2}" : "{1}";
            this.ImageUrl = string.Format(format, "", ProfileOperations.GetAvatarImagePath(_userId, _type, null), DateTime.Now.Ticks);
        }
    }
}
